package Hash;

import java.util.HashMap;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-01
 * Time:14:31
 * Text：219. 存在重复元素 II
 */
public class Text1 {
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            if(map.getOrDefault(nums[i],-1)==-1){
                map.put(nums[i],i);
            }else{
                if(Math.abs(map.get(nums[i])-i)<=k)
                    return true;
                else map.put(nums[i],i);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr={1,0,1,1};
        containsNearbyDuplicate(arr,1);
    }
}
